package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.fenghongzhang.day003.db.DaoMaster;
import com.fenghongzhang.day003.db.DaoSession;
import com.fenghongzhang.day003.db.StudentDao;
import com.fenghongzhang.day003.db.UserDao;

import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = "Main2Activity";

    private DaoSession daoSession;
    private UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        DaoMaster.DevOpenHelper a1901 = new DaoMaster.DevOpenHelper(this, "a1901");
        SQLiteDatabase writableDatabase = a1901.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(writableDatabase);
        daoSession = daoMaster.newSession();
        userDao = daoSession.getUserDao();
        StudentDao studentDao = daoSession.getStudentDao();
    }

    public void btn(View view) {
        User user = new User();
        user.setName("xiaoming");
        user.setAge(18);
//        daoSession.insert(user);
        userDao.insert(user);
    }

    public void query(View view) {
//        List<User> users = daoSession.loadAll(User.class);
//        List<User> users = userDao.loadAll();
//        Toast.makeText(this, users.size()+"", Toast.LENGTH_SHORT).show();

//        List<User> users = userDao.queryRaw("where _id=?", "3");
//        List<User> users = userDao.queryRaw("where name=? and age = ?", "daming","18");
//        String name = users.get(0).getName();
//        Toast.makeText(this, name, Toast.LENGTH_SHORT).show();

        int ye = 2;
        int offset = 0;
        List<User> list = userDao.queryBuilder().offset(offset*2).limit(ye).list();
        for (int i = 0; i < list.size(); i++) {
            Long id = list.get(i).getId();
            Log.i(TAG, "query: "+id);

        }
        offset++;

        List<User> list1 = userDao.queryBuilder().offset(offset*2).limit(ye).list();
        for (int i = 0; i < list1.size(); i++) {
            Long id = list1.get(i).getId();
            Log.i(TAG, "query: "+id);

        }
        offset++;




    }

    public void del(View view) {
//        userDao.deleteAll();
        User user = new User();
        user.setId(2l);
        user.setName("xiaoming");
        user.setAge(18);
        userDao.delete(user);
    }

    public void update(View view) {
        User user = new User();
        user.setId(3l);
        user.setName("daming");
        user.setAge(18);
        userDao.update(user);
    }
}
